
void An4()
{
    pinMode(buttonPin, OUTPUT);
    digitalWrite(buttonPin, LOW);
    delay(100);
    pinMode(buttonPin, INPUT);
}

void ZYB_onoff(bool c)
{
    if (c && !ZYB_State && config.QF_State)
    {
        Serial.println("ZYB_on");
        An4();
        ZYB_State = c;
    }
    else if (!c && ZYB_State)
    {
        Serial.println("ZYB_off");
        An4();
        ZYB_State = c;
    }
}

void QF_onoff(bool c)
{
    digitalWrite(LedBlink, HIGH); // 切换指示灯状态
    delay(100);
    digitalWrite(LedBlink, LOW); // 切换指示灯状态
    if (c)
    {
        Serial.println("QF_on");
        digitalWrite(QFkaiPin, HIGH);
    }
    else
    {
        Serial.println("QF_off");
        digitalWrite(QFguanPin, HIGH);
    }
    config.QF_State = c;
    delay(5000);
    digitalWrite(QFkaiPin, LOW);
    digitalWrite(QFguanPin, LOW);
}

void QF_onoff()
{
    static int i = 0;
    if (!digitalRead(buttonPin))
    {
        i++;
        if (i > 3000)
        {
            QF_onoff(!config.QF_State);
        }
    }
    else
    {
        i = 0;
    }
}


void autoZengYa()
{
    static unsigned long ZY_Time = 0;
    static bool ZY_State = false;
    static bool QF_State = config.QF_State;
    static bool Auto_State = config.Auto_State;
    
    WiF_State = !digitalRead(liuShuiPin);
    ZYB_State = digitalRead(ZYBStatePin);

    if (config.QF_State && config.Auto_State)
    {
        if (!ZYB_State && ShuiYaValue < 1.00)
        {
            ZYB_onoff(true);
        }
        else if (ZYB_State && ShuiYaValue > 2.00)
        {
            ZYB_onoff(false);
        }
    }

    if (ZYB_State) // 打开增压后检测流水状态
    {
        if (!WiF_State) // 如果没有水
        {
            if (millis() - ZY_Time >= 10000) // 超时后关闭增压泵
            {
                ZYB_onoff(WiF_State);
            }
        }
        else
        {
            ZY_Time = millis();
        }
    }
    else
    {
        ZY_Time = millis();
    }

    if (ZY_State != ZYB_State) // 发送增压泵的状态
    {
        sendtoTCPServer();
        ZY_State = ZYB_State;
        // doHttpStick(ZYB_State ? "增压已打开" : "增压已关闭");
    }

    if (Auto_State != config.Auto_State) // 发送增压泵的状态
    {
        sendtoTCPServer();
        Auto_State = config.Auto_State;
        saveConfig();
        // doHttpStick(ZYB_State ? "自动已打开" : "自动已关闭");
    }

    if (QF_State != config.QF_State) // 发送球阀的状态
    {
        sendtoTCPServer("cmd=2&uid=" + UID + "&topic=" + TOPIC2 + "/up&msg=" + (config.QF_State ? "on" : "off") + "\r\n"); // 推送消息
        QF_State = config.QF_State;
        saveConfig();
        // doHttpStick(config.QF_State ? "球阀已打开" : "球阀已关闭");
    }
}

void ZengYaBang()
{
    if (millis() - checkPinStatusTick >= 100)
    {
        QF_onoff();
        autoZengYa();
        checkPinStatusTick = millis();
    }
}
